/*
 * Please refer below document for further detail or extension
 * http://download.oracle.com/docs/cd/E13222_01/wls/docs90/wlsmbeanref/core/index.html
 */
package monitor.connector;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Hashtable;

import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.Context;

import common.Conf;

public class Connector {
	
	private static MBeanServerConnection connection;
	private static JMXConnector connector;
	private static ObjectName service;
	
	public static MBeanServerConnection getJmxConnection(){
		
		String hostname = Conf.getConf("hostName");
		String portString = Conf.getConf("portString");
		String username = Conf.getConf("userName");
		String password = Conf.getConf("passWord");
		
		initConnection(hostname, portString, username, password);
		
		return connection;
	}
	
	public static void closeJmxConnection(){
		
		try{
			connector.close();
		}catch(IOException e){
			e.printStackTrace();
		}
	}
	
	public static void initConnection(String hostname, String portString, String username, String password) {
		
		String protocol = "t3";
		int port = Integer.valueOf(portString).intValue();
		String jndiroot = "/jndi/";
		String mserver = "weblogic.management.mbeanservers.domainruntime";
//		String mserver = "weblogic.management.mbeanservers.runtime";
		try{
			
			JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname, port, jndiroot + mserver);
			
			Hashtable h = new Hashtable();
			h.put(Context.SECURITY_PRINCIPAL, username);
			h.put(Context.SECURITY_CREDENTIALS, password);
			h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,"weblogic.management.remote");
			h.put("jmx.remote.x.request.waiting.timeout", new Long(10000));
			
			connector = JMXConnectorFactory.connect(serviceURL, h);
			connection = connector.getMBeanServerConnection();
			
		}catch(MalformedURLException e){
			e.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	
	public static Integer getJDBCDataSourceRuntimeMBeanAttribute(String attribute){
		
		Integer count = new Integer(0);
		connection = Connector.getJmxConnection();
		
		try{
			
			String objName = "com.bea:Name="
							+ Conf.getConf("ApplicationRuntime") 
							+",ServerRuntime="
							+Conf.getConf("ServerRuntime")
							+",Location="
							+Conf.getConf("ServerRuntime")
							+",ApplicationRuntime="
							+ Conf.getConf("ApplicationRuntime")
							+",Type=JDBCDataSourceRuntime";
			
			service = new ObjectName(objName);
				
			count = (Integer)connection.getAttribute(service, attribute);
			
		}catch(MalformedObjectNameException e){
			e.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}
		
		Connector.closeJmxConnection();
		return count;
	}	
	
	public static Object getThreadPoolRuntimeMBeanAttribute(String attribute){
		
		Object rtnObj = null;
		connection = Connector.getJmxConnection();
		
		try{
			//[MBeanServerInvocationHandler]com.bea:ServerRuntime=GerwapServer,Name=ThreadPoolRuntime,Type=ThreadPoolRuntime
			//[MBeanServerInvocationHandler]com.bea:ServerRuntime=examplesServer,Name=ThreadPoolRuntime,Type=ThreadPoolRuntime
			
			//com.bea:Name=ThreadPoolRuntime,ServerRuntime=examplesServer,Location=examplesServer,Type=ThreadPoolRuntime
			String objName = "com.bea:Name=ThreadPoolRuntime"
					+",ServerRuntime="
					+Conf.getConf("ServerRuntime")
					+",Location="
					+Conf.getConf("ServerRuntime")
					+",Type=ThreadPoolRuntime";
			
			service = new ObjectName(objName);
				
			rtnObj = connection.getAttribute(service, attribute);
			
		}catch(MalformedObjectNameException e){
			e.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}
		
		Connector.closeJmxConnection();
		return rtnObj;
	}		
	

	public static Integer getWebAppComponentRuntimeMBean(String attribute){
		
		Integer count = new Integer(0);
		connection = Connector.getJmxConnection();
		
		try{
			/*
			 * ServerRuntime = examplesServer
				#serverName = GerwapServer
				
				ApplicationRuntime = eu-stgdb 
				#ApplicationRuntime = jdbc/oraclePool 
				
				Location = examplesServer
				#Location = GerwapServer
			 * 
			 */
			//com.bea:Name=examplesServer_/examplesWebApp,ServerRuntime=examplesServer,Location=examplesServer,ApplicationRuntime=examplesWebApp,Type=WebAppComponentRuntime
			String objName = "com.bea:Name=kimchi_/WebContent"
					+",ServerRuntime="
					+Conf.getConf("ServerRuntime")
					+",Location="
					+Conf.getConf("ServerRuntime")
					+",ApplicationRuntime=WebContent"
					+",Type=WebAppComponentRuntime";
			
			service = new ObjectName(objName);
				
			count = (Integer)connection.getAttribute(service, attribute);
			
		}catch(MalformedObjectNameException e){
			e.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}
		
		Connector.closeJmxConnection();
		return count;
	}		

}
